package cn.maydaymemory.maydaycore.command.api;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public abstract class CommandStructure {
    protected CommandSender sender;
    protected Player player;
    protected String label;
    protected String[] args;
    private static Map<CommandStructure,CommandStructureExecutor> executors = new HashMap<>();
    public void redefine(CommandSender sender,String label,String[] args,Method method) throws InvocationTargetException, IllegalAccessException {
        this.sender=sender;
        if(sender instanceof Player) player = (Player) sender;
        else player=null;
        this.label=label;
        this.args=args;
        access();
        if(method!=null) method.invoke(this);
    }
    public CommandStructureExecutor getChildStructureExecutor(CommandStructure child){
        CommandStructureExecutor executor= executors.get(child);
        if(executor == null) {
            executor=new CommandStructureExecutor(child);
            executors.put(child,executor);
        }
        return executor;
    }
    protected abstract void access();

}
